/**
 * @Author MrsGuo
 * version 1.0
 * @Date 2023-08-18 15:03
 */
package com.cl.erp_2301.controller;


import com.cl.erp_2301.Service.EmpRoleService;
import com.cl.erp_2301.Service.EmpService;
import com.cl.erp_2301.dto.DataDTO;
import com.cl.erp_2301.dto.EmpDTO;
import com.cl.erp_2301.dto.MenuDTO;
import com.cl.erp_2301.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v2")
public class EmController {

    @Resource
    private EmpService empService;
    @Resource
    private EmpRoleService empRoleService;

    @RequestMapping(value = "/emp_listByPage/listByPage", method = RequestMethod.GET)
    public Result findAll(int page, int rows) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<EmpDTO> storeDtoList = empService.findAll();
        DataDTO data1 = new DataDTO();
        data1.setRows(storeDtoList);
        data1.setTotal(page1.getTotal());
        return Result.ok(data1);
    }

    @PostMapping("/emp_updatePwd_reset/update")
    public Result updateById(@RequestParam("id") Integer uuid,
                             @RequestParam("newPwd") String newPwd, EmpDTO empDTO) {
        empDTO.setUuid(uuid);
        empDTO.setNewPwd(newPwd);
        empService.updateById(empDTO);
        return Result.ok();
    }


    @RequestMapping(value = "/role/listByPage", method = RequestMethod.GET)
    public Result findByN(@RequestParam(value = "name", required = false, defaultValue = "") String name, int page, int rows) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<EmpDTO> storeDtoList = empRoleService.findByN(name);
        DataDTO data1 = new DataDTO();
        data1.setRows(storeDtoList);
        data1.setTotal(page1.getTotal());
        return Result.ok(data1);
    }

    @RequestMapping(value = "/role/selectone", method = RequestMethod.GET)
    public Result ff(HttpServletRequest request, @RequestParam("id") Integer id) {
        EmpDTO roleDTO = new EmpDTO();
        roleDTO.setId(id);
        request.getSession().setAttribute("id", roleDTO.getId());
        return Result.ok();
    }

    @PostMapping("/role/update")
    public Result updateId(@RequestParam("name") String name, EmpDTO empRoleDTO, HttpSession session) {
        Integer uuid = (Integer) session.getAttribute("id");
        empRoleDTO.setUuid(uuid);
        empRoleDTO.setName(name);
        empRoleService.updateId(empRoleDTO);
        return Result.ok();
    }

    @RequestMapping(value = "/role", method = RequestMethod.DELETE)
    public Result deleteById(@RequestParam(value = "id", required = false, defaultValue = "") Integer id) {
        empRoleService.deleteById(id);
        return Result.ok();
    }

    @PostMapping("/role/add")
    public Result addRole(@RequestParam("name") String name) {
        EmpDTO empRoleDTO = new EmpDTO();
        empRoleDTO.setName(name);
        empRoleService.addRole(empRoleDTO);
        return Result.ok();
    }

    @PostMapping("/role_readRoleMenus")
    public Result unKnow(@RequestParam("id") Integer id) {
        List<MenuDTO> men = empRoleService.unKnow(id);
        DataDTO dataDto = new DataDTO();
        dataDto.setRows(men);
        return Result.ok(dataDto);
    }

    @PostMapping("/role_list")
    public Result findR(@RequestParam(value = "name", required = false, defaultValue = "") String name, int page, int rows) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<EmpDTO> storeDtoList = empRoleService.findByN(name);
        DataDTO data1 = new DataDTO();
        data1.setRows(storeDtoList);
        data1.setTotal(page1.getTotal());
        return Result.ok(data1);
    }

    @PostMapping("/emp_list")
    public Result findEmp(int page, int rows, HttpSession session) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<EmpDTO> storeDtoList = empRoleService.findEmp();
        DataDTO data1 = new DataDTO();
        data1.setRows(storeDtoList);
        data1.setTotal(page1.getTotal());
        return Result.ok(data1);
    }


    @PostMapping("/emp_readEmpRoles")
    public Result findByMen(@RequestParam("id") Integer uuid, HttpServletRequest request, EmpDTO empRoleDTO) {
        List<MenuDTO> men = empRoleService.findByMen(uuid);
        DataDTO dataDto = new DataDTO();
        dataDto.setRows(men);
        return Result.ok(dataDto);
    }

    @PostMapping("/emp_updateEmpRoles")
    public Result updateRo() {
        return Result.ok();
    }

}
